using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using star_questionnair.Models;
using star_questionnair.Models.Enum;
using star_questionnairquestionnair.Services;

namespace star_questionnair.Services.impl;

public class DifferenceChoiceQuestionAnalysisService : IDifferenceChoiceQuestionAnalysisService
{
     private readonly IAnswerService _answerService;
     private readonly IQuestionService _questionService;
     private readonly IHistoryService _historyService;
     private readonly IUserService _userService;
     private readonly IResponseService _responseService;

        public DifferenceChoiceQuestionAnalysisService(
            IAnswerService answerService,
            IQuestionService questionService,
            IHistoryService historyService,
            IUserService userService,
            IResponseService responseService)
        {
            _answerService = answerService;
            _questionService = questionService;
            _historyService = historyService;
            _userService = userService;
            _responseService = responseService;
        }

        // 性别差异化分析
        public async Task<Dictionary<string, Dictionary<bool, int>>> GetGenderDifferenceAsync(int questionId)
        {
            var question = await _questionService.GetQuestionByIdAsync(questionId);

            // 判断是否为选择题类型
            if (!QuestionEnumExtensions.IsChoiceType(question.Type))
            {
                return null;
            }

            var options =  _historyService.GetOptionsByQuestionId(questionId);
            var result = new Dictionary<string, Dictionary<bool, int>>();

            foreach (var option in options)
            {
                var difference = new Dictionary<bool, int>();
                var answers =  _answerService.GetAnswersByOptionId(option.Id);

                foreach (var answer in answers)
                {
                    var user =  _answerService.GetUserByAnswer(answer);
                    difference[user.Gender] = difference.GetValueOrDefault(user.Gender, 0) + 1;
                }

                result[option.Content] = difference;
            }

            return result;
        }

        // 年龄差异化分析
        public async Task<Dictionary<string, Dictionary<string, int>>> GetAgeDifferenceAsync(int questionId)
        {
            var question = await _questionService.GetQuestionByIdAsync(questionId);

            // 判断是否为选择题类型
            if (!QuestionEnumExtensions.IsChoiceType(question.Type))
            {
                return null;
            }

            var options =  _historyService.GetOptionsByQuestionId(questionId);
            var result = new Dictionary<string, Dictionary<string, int>>();
            var ageStages = new[] { "20-25", "25-30", "30-35", "35-40", "40-45", "45-50", "50+" };

            foreach (var option in options)
            {
                var difference = new Dictionary<string, int>();
                var answers =  _answerService.GetAnswersByOptionId(option.Id);

                foreach (var answer in answers)
                {
                    var user =  _answerService.GetUserByAnswer(answer);
                    var age = await _userService.CalculateUserAgeAsync(user.Id);
                    string ageStage = "-1";

                    if (age >= 20 && age <= 25) ageStage = "20-25";
                    else if (age > 25 && age <= 30) ageStage = "25-30";
                    else if (age > 30 && age <= 35) ageStage = "30-35";
                    else if (age > 35 && age <= 40) ageStage = "35-40";
                    else if (age > 40 && age <= 45) ageStage = "40-45";
                    else if (age > 45 && age <= 50) ageStage = "45-50";
                    else if (age > 50) ageStage = "50+";
                    
                    difference[ageStage] = difference.GetValueOrDefault(ageStage, 0) + 1;
                }

                result[option.Content] = difference;
            }

            return result;
        }

        // 公司差异化分析
        public async Task<Dictionary<string, Dictionary<string, int>>> GetEnterpriseDifferenceAsync(int questionId)
        {
            var question = await _questionService.GetQuestionByIdAsync(questionId);

            // 判断是否为选择题类型
            if (!QuestionEnumExtensions.IsChoiceType(question.Type))
            {
                return null;
            }

            var options =  _historyService.GetOptionsByQuestionId(questionId);
            var result = new Dictionary<string, Dictionary<string, int>>();

            foreach (var option in options)
            {
                var difference = new Dictionary<string, int>();
                var answers =  _answerService.GetAnswersByOptionId(option.Id);

                foreach (var answer in answers)
                {
                    var user =  _answerService.GetUserByAnswer(answer);
                    difference[user.Enterprise] = difference.GetValueOrDefault(user.Enterprise, 0) + 1;
                }

                result[option.Content] = difference;
            }

            return result;
        }

        // 职位差异化分析
        public async Task<Dictionary<string, Dictionary<string, int>>> GetRoleDifferenceAsync(int questionId)
        {
            var question = await _questionService.GetQuestionByIdAsync(questionId);

            // 判断是否为选择题类型
            if (!QuestionEnumExtensions.IsChoiceType(question.Type))
            {
                return null;
            }

            var options =  _historyService.GetOptionsByQuestionId(questionId);
            var result = new Dictionary<string, Dictionary<string, int>>();

            foreach (var option in options)
            {
                var difference = new Dictionary<string, int>();
                var answers =  _answerService.GetAnswersByOptionId(option.Id);

                foreach (var answer in answers)
                {
                    var user =  _answerService.GetUserByAnswer(answer);
                    difference[user.RoleName] = difference.GetValueOrDefault(user.RoleName, 0) + 1;
                }

                result[option.Content] = difference;
            }

            return result;
        }

        // 整个问卷性别差异化分析
        public async Task<Dictionary<bool, int>> GetGenderDifferenceBigAsync(int questionnaireId)
        {
            var response = new Response { QuestionnaireId = questionnaireId };
            var result = new Dictionary<bool, int>();

            var responses =  _responseService.SelectAll(response);

            foreach (var r in responses)
            {
                var user =  _responseService.SelectUser(r);
                result[user.Gender] = result.GetValueOrDefault(user.Gender, 0) + 1;
            }

            return result;
        }

        // 整个问卷年龄差异化分析
        public async Task<Dictionary<string, int>> GetAgeDifferenceBigAsync(int questionnaireId)
        {
            var response = new Response { QuestionnaireId = questionnaireId };
            var result = new Dictionary<string, int>();

            var responses =  _responseService.SelectAll(response);

            foreach (var r in responses)
            {
                var user =  _responseService.SelectUser(r);
                var age = await _userService.CalculateUserAgeAsync(user.Id);
                string ageStage = null;

                if (age >= 20 && age <= 25) ageStage = "20-25";
                else if (age > 25 && age <= 30) ageStage = "25-30";
                else if (age > 30 && age <= 35) ageStage = "30-35";
                else if (age > 35 && age <= 40) ageStage = "35-40";
                else if (age > 40 && age <= 45) ageStage = "40-45";
                else if (age > 45 && age <= 50) ageStage = "45-50";
                else if (age > 50) ageStage = "50+";

                result[ageStage] = result.GetValueOrDefault(ageStage, 0) + 1;
            }

            return result;
        }

        // 整个问卷公司差异化分析
        public async Task<Dictionary<string, int>> GetEnterpriseDifferenceBigAsync(int questionnaireId)
        {
            var response = new Response { QuestionnaireId = questionnaireId };
            var result = new Dictionary<string, int>();

            var responses =  _responseService.SelectAll(response);

            foreach (var r in responses)
            {
                var user =  _responseService.SelectUser(r);
                result[user.Enterprise] = result.GetValueOrDefault(user.Enterprise, 0) + 1;
            }

            return result;
        }

        // 整个问卷职位差异化分析
        public async Task<Dictionary<string, int>> GetRoleDifferenceBigAsync(int questionnaireId)
        {
            var response = new Response { QuestionnaireId = questionnaireId };
            var result = new Dictionary<string, int>();

            var responses =  _responseService.SelectAll(response);

            foreach (var r in responses)
            {
                var user =  _responseService.SelectUser(r);
                result[user.RoleName] = result.GetValueOrDefault(user.RoleName, 0) + 1;
            }

            return result;
        }
}